package com.hyg.lib_music.ui.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.hyg.lib_base.mainUtils.StatusBarUtil;
import com.hyg.lib_common.Base.BaseActivity;
import com.hyg.lib_common.DataModel.EventBusMessage;
import com.hyg.lib_music.R;
import com.hyg.lib_music.adapter.MusicTesttingAnswerAdapter;
import com.hyg.lib_common.DataModel.testting.TesttingAnswer;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;
import java.util.Random;

public class TesttingTizhi1Activity extends BaseActivity {
    ArrayList<TesttingAnswer> list0;
    ArrayList<TesttingAnswer> list;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_music_testting_tizhi1);
        StatusBarUtil.setStatusBar(1, this, getResources().getColor(R.color.white), 60, null, null, true);
        init();
    }

    public void init() {
        initData();
        initView();
    }

    public void initData() {
        list0 = getIntent().getParcelableArrayListExtra("list");
        list = new ArrayList<>();
        String[] answer = getResources().getStringArray(R.array.music_tizhi_answer1);
        for (String string : answer) {
            list.add(new TesttingAnswer(string, false));
        }
    }


    public void initView() {
        ImageView backBtn = findViewById(R.id.back);
        TextView titleTv = findViewById(R.id.tv_title);
        TextView infoTv = findViewById(R.id.tv_title_more);

        titleTv.setText("体质辨识问卷");
        infoTv.setText("问卷介绍");
        infoTv.setVisibility(View.VISIBLE);
        RecyclerView answerRcv = findViewById(R.id.rcv_answer);
//        LinearLayoutManager layoutManager = new LinearLayoutManager(this);
        GridLayoutManager layoutManager = new GridLayoutManager(this, 3);
//        MusicTesttingAnswerAdapter adapter = new MusicTesttingAnswerAdapter(this, list);
        MusicTesttingAnswerAdapter adapter = new MusicTesttingAnswerAdapter(this, list);
        answerRcv.setLayoutManager(layoutManager);
        answerRcv.setAdapter(adapter);

        infoTv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(TesttingTizhi1Activity.this, TesttingIntroActivity.class);
                intent.putExtra("type", "1");
                startActivity(intent);
            }
        });
        backBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }


    public void onReturn(View view) {
        finish();
    }


    public void onFinish(View view) {
        Intent intent = new Intent(TesttingTizhi1Activity.this, TestingResultActivity.class);
        intent.putExtra("type", "1");
        intent.putExtra("result", parseResult());
        startActivity(intent);
        EventBus.getDefault().post(new EventBusMessage("testTZfinish"));
        finish();
    }

    public String parseResult() {
        String result = "平和质";
        if (list0 == null || list == null) return result;
        ArrayList<String> arrayList = new ArrayList<>();
        for (TesttingAnswer answer : list0) {
            if (answer.isChoose() && !answer.getAnswer().equalsIgnoreCase("全都没有")) {
                arrayList.add(answer.getAnswer());
            }
        }
        for (TesttingAnswer answer : list) {
            if (answer.isChoose() && !answer.getAnswer().equalsIgnoreCase("全都没有")) {
                arrayList.add(answer.getAnswer());
            }
        }
        if (arrayList.size() == 0) {
            return result;
        }
        if (arrayList.contains("容易过敏")) {
            result = "特禀质";
            return result;
        }
        if (arrayList.contains("容易怕冷")) {
            result = "阳虚质";
            return result;
        }

        ArrayList<String> resultList = new ArrayList<>();
        if (arrayList.contains("嘴里发粘") || arrayList.contains("咽喉痰多")) {
            resultList.add("痰湿质");
        }
        if (arrayList.contains("面色暗沉") || arrayList.contains("皮肤青紫瘀斑")) {
            resultList.add("血瘀质");
        }
        if (arrayList.contains("情绪低落") || arrayList.contains("胀闷胀痛")) {
            resultList.add("气郁质");
        }
        if (arrayList.contains("爱出虚汗") && arrayList.contains("容易疲劳") && arrayList.contains("反复感冒")) {
            resultList.add("气虚质");
        }
        if (arrayList.contains("大便粘滞") && arrayList.contains("口有异味") && arrayList.contains("面部出油")) {
            resultList.add("湿热质");
        }
        if (arrayList.contains("容易口干") && arrayList.contains("大便干燥") && arrayList.contains("手脚心热")) {
            resultList.add("阴虚质");
        }

        if (resultList.size() > 0) {
            int index = new Random().nextInt(resultList.size());
            return resultList.get(index);
        } else {
            if ((arrayList.contains("爱出虚汗") && arrayList.contains("容易疲劳")) || (arrayList.contains("反复感冒") && arrayList.contains("容易疲劳"))
                    || (arrayList.contains("爱出虚汗") && arrayList.contains("反复感冒"))
            ) {
                resultList.add("气虚质");
            }
            if ((arrayList.contains("大便粘滞") && arrayList.contains("口有异味")) || (arrayList.contains("面部出油") && arrayList.contains("口有异味"))
                    || (arrayList.contains("大便粘滞") && arrayList.contains("面部出油"))
            ) {
                resultList.add("湿热质");
            }
            if ((arrayList.contains("容易口干") && arrayList.contains("大便干燥")) || (arrayList.contains("手脚心热") && arrayList.contains("大便干燥"))
                    || (arrayList.contains("容易口干") && arrayList.contains("手脚心热"))
            ) {
                resultList.add("阴虚质");
            }
            if (resultList.size() > 0) {
                int index = new Random().nextInt(resultList.size());
                return resultList.get(index);
            }
        }
        return result;
    }

}